package com.kaily.image.share.share;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.TextUtils;

import com.kaily.image.R;
import com.kaily.imagelib.IMergeImageElement;
import com.kaily.imagelib.MergeImage;
import com.kaily.imagelib.element.AvatarElement;
import com.kaily.imagelib.element.ImageElement;
import com.kaily.imagelib.element.QRCodeImageElement;
import com.kaily.imagelib.element.TextElement;
import com.kaily.imagelib.utils.ImageUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用图片分享：
 *
 * @date 2018/12/6 下午2:56
 * @author: kaily_zhou
 */
public class CommonImageShareMerge implements ICommonImageShareMerge {
    private Activity context;
    private MergeImage mergeImage;

    public CommonImageShareMerge(Activity context) {
        this.context = context;
        mergeImage = new MergeImage();
    }

    @Override
    public void mergeImage(final ImageShareInfo imageShareInfo, final MergeImage.MergeCallBack mergeCallBack) {
        if (imageShareInfo == null) {
            if (mergeCallBack != null) {
                mergeCallBack.callback(null);
            }
            return;
        }
        mergeImage.cancelMerge();
        mergeImage.startMerge(context, new MergeImage.MergeCallBack() {

            @Override
            public Bitmap mergeProcess() {
                //创建合成图片所需要的属性
                ImageMergeAttr imageMergeAttr = createImageMergeAttr();
                //创建合成图片所需要的元素
                List<IMergeImageElement> mergeImageElements = createMergeImageElements(imageShareInfo, imageMergeAttr);
                //根据图片元素集合进行图片合成
                Bitmap shareBitmap = mergeImageElement(imageMergeAttr.width, imageMergeAttr.height, mergeImageElements);
                return shareBitmap;
            }

            @Override
            public void callback(Bitmap bitmap) {
                if (mergeCallBack != null) {
                    mergeCallBack.callback(bitmap);
                }
            }
        });
    }

    /**
     * 创建合成所需要的元素对象
     *
     * @param imageShareInfo
     * @param imageMergeAttr
     * @return
     */
    private List<IMergeImageElement> createMergeImageElements(ImageShareInfo imageShareInfo,
                                                              ImageMergeAttr imageMergeAttr) {
        if (imageMergeAttr == null || imageShareInfo == null) {
            return null;
        }

        //图片元素
        ImageElement imageElement = createImageElement(imageShareInfo.getImgUrl(),
                imageMergeAttr.imgLeft, imageMergeAttr.imgTop,
                imageMergeAttr.imgWidth, imageMergeAttr.imgHeight, true);
        if (imageElement == null) {
            return null;
        }
        //更新属性
        updateImageMergeAttr(imageMergeAttr, imageElement.getHeight());
        //头像元素
        AvatarElement avatarElement = createAvatarElement(
                imageMergeAttr.avatarX, imageMergeAttr.avatarY,
                imageMergeAttr.avatarWidth, imageMergeAttr.avatarHeight, imageShareInfo.getAvater());
        if (avatarElement == null) {
            return null;
        }
        //昵称
        TextElement nickNameElement = null;
        String userName = imageShareInfo.getUserName();
        if (!TextUtils.isEmpty(userName)) {
            nickNameElement = createTextElement(ImageUtils.CUSTOMER_NAME_PREFIX + userName,
                    imageMergeAttr.nickNameFontSize, imageMergeAttr.nickNameFontColor,
                    imageMergeAttr.nickNameLeft, imageMergeAttr.nickNameTop,
                    imageMergeAttr.nickNameWidth, true);
            if (nickNameElement == null) {
                return null;
            }
        }
        //二维码元素
        QRCodeImageElement qrCodeImageElement = createQRCodeImageElement(imageShareInfo.getQrCodeUrl(),
                imageMergeAttr.qrCodeLeft, imageMergeAttr.qrCodeTop,
                imageMergeAttr.qrCodeWidth, imageMergeAttr.qrCodeHeight);
        if (qrCodeImageElement == null) {
            return null;
        }
        //主要文本
        String primaryText = imageShareInfo.getPrimaryText();
        TextElement primaryTextElement = null;
        if (!TextUtils.isEmpty(primaryText)) {
            primaryTextElement = createTextElement(primaryText,
                    imageMergeAttr.primaryTextFontSize, imageMergeAttr.primaryTextFontColor,
                    imageMergeAttr.primaryTextLeft, imageMergeAttr.primaryTextTop,
                    imageMergeAttr.primaryTextWidth, true);
            if (primaryTextElement == null) {
                return null;
            }
        }
        //次要文本
        int secondaryTextTop = imageMergeAttr.secondaryTextTop
                + (primaryTextElement == null ? 0 : primaryTextElement.getHeight()) + 4;
        String secondaryText = imageShareInfo.getSecondaryText();
        TextElement secondaryTextElement = null;
        if (!TextUtils.isEmpty(secondaryText)) {
            secondaryTextElement = createTextElement(secondaryText,
                    imageMergeAttr.secondaryTextFontSize, imageMergeAttr.secondaryTextFontColor,
                    imageMergeAttr.secondaryTextLeft, secondaryTextTop,
                    imageMergeAttr.secondaryTextWidth, false);
            if (secondaryTextElement == null) {
                return null;
            }
        }

        List<IMergeImageElement> iMergeImageElements = new ArrayList<>();
        iMergeImageElements.add(imageElement);
        iMergeImageElements.add(avatarElement);
        iMergeImageElements.add(nickNameElement);
        iMergeImageElements.add(qrCodeImageElement);
        iMergeImageElements.add(primaryTextElement);
        iMergeImageElements.add(secondaryTextElement);
        return iMergeImageElements;
    }

    /**
     * 根据图片上Element合成图片
     *
     * @param sourceBitmapWidth
     * @param sourceBitmapHeight
     * @param imageElements
     * @return
     */
    private Bitmap mergeImageElement(int sourceBitmapWidth, int sourceBitmapHeight,
                                     List<IMergeImageElement> imageElements) {
        if (sourceBitmapHeight <= 0 || sourceBitmapWidth <= 0) {
            return null;
        }
        if (imageElements == null || imageElements.isEmpty()) {
            return null;
        }
        Bitmap shareBitmap = Bitmap.createBitmap(sourceBitmapWidth, sourceBitmapHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(shareBitmap);
        canvas.drawColor(Color.WHITE);
        for (IMergeImageElement mergeImageElement : imageElements) {
            if (mergeImageElement != null) {
                boolean result = mergeImageElement.mergeImage(canvas);
                if (!result) {
                    return null;
                }
            }
        }
        return shareBitmap;
    }

    /**
     * 创建ImageElement元素
     *
     * @param imgUrl
     * @param imgLeft
     * @param imgTop
     * @param imgWidth
     * @param imgHeight
     * @param isWidthStandard
     * @return
     */
    private ImageElement createImageElement(String imgUrl, int imgLeft, int imgTop,
                                            int imgWidth, int imgHeight, boolean isWidthStandard) {
        if (TextUtils.isEmpty(imgUrl)) {
            return null;
        }
        ImageElement imageElement = new ImageElement(imgUrl,
                true, imgLeft, imgTop,
                imgWidth, imgHeight, ImageElement.IMG_LOAD_TYPE_GLIDE, isWidthStandard);
        boolean imageResult = imageElement.generateElement();
        if (!imageResult || imageElement.getHeight() <= 0) {
            return null;
        }
        return imageElement;
    }

    /**
     * 创建头像元素
     *
     * @param avatarX
     * @param avatarY
     * @param avatarWidth
     * @param avatarHeight
     * @return
     */
    private AvatarElement createAvatarElement(int avatarX, int avatarY,
                                              int avatarWidth, int avatarHeight, String avater) {
        //图片元素
        Bitmap headBitMap = BitmapFactory.decodeResource(context.getResources(),
                R.mipmap.ic_launcher);
        AvatarElement avatarElement = new AvatarElement(headBitMap, avater, true,
                avatarX, avatarY,
                avatarWidth, avatarHeight, true);
        boolean avatarResult = avatarElement.generateElement();
        if (!avatarResult) {
            return null;
        }
        return avatarElement;
    }

    /**
     * 二维码元素
     *
     * @param qrCodeUrl
     * @param qrCodeLeft
     * @param qrCodeTop
     * @param qrCodeWidth
     * @param qrCodeHeight
     * @return
     */
    private QRCodeImageElement createQRCodeImageElement(String qrCodeUrl,
                                                        int qrCodeLeft, int qrCodeTop,
                                                        int qrCodeWidth, int qrCodeHeight) {
        qrCodeUrl = (TextUtils.isEmpty(qrCodeUrl) ? "  " : qrCodeUrl);
        Bitmap qrIconBitMap = BitmapFactory.decodeResource(context.getResources(),
                R.drawable.icon);
        QRCodeImageElement qrCodeImageElement = new QRCodeImageElement("", qrCodeUrl,
                qrIconBitMap, true,
                qrCodeLeft, qrCodeTop, qrCodeWidth, qrCodeHeight);
        boolean qrCodeResult = qrCodeImageElement.generateElement();
        if (!qrCodeResult) {
            return null;
        }
        return qrCodeImageElement;
    }


    /**
     * 创建文本元素
     *
     * @param text
     * @param fontSize
     * @param fontColor
     * @param textLeft
     * @param textTop
     * @param textWidth
     * @return
     */
    private TextElement createTextElement(String text,
                                          float fontSize, int fontColor,
                                          int textLeft, int textTop,
                                          int textWidth, boolean isBoldFont) {
        text = TextUtils.isEmpty(text) ? "" : text;
        TextElement textElement = new TextElement(text,
                fontSize, fontColor, textLeft, textTop,
                0, textWidth, true, isBoldFont);
        boolean textResult = textElement.generateElement();
        if (!textResult) {
            return null;
        }
        return textElement;
    }


    /**
     * 创建合成图片所需要的属性对象
     *
     * @return
     */
    private ImageMergeAttr createImageMergeAttr() {
        ImageMergeAttr imageMergeAttr = new ImageMergeAttr();
        imageMergeAttr.width = CommonImageConfigure.DEFAULT_STANDARD_WIDTH;
        //图片属性
        imageMergeAttr.imgWidth = imageMergeAttr.width - CommonImageConfigure.DEFAULT_IMG_PADDING * 2;
        imageMergeAttr.imgLeft = CommonImageConfigure.DEFAULT_IMG_PADDING;
        imageMergeAttr.imgTop = CommonImageConfigure.DEFAULT_IMG_PADDING;
        //头像属性
        imageMergeAttr.avatarHeight = CommonImageConfigure.DEFAULT_AVATAR_SIZE;
        imageMergeAttr.avatarWidth = CommonImageConfigure.DEFAULT_AVATAR_SIZE;
        imageMergeAttr.avatarX = CommonImageConfigure.DEFAULT_BOTTOM_PADDING;
        //昵称
        imageMergeAttr.nickNameFontSize = CommonImageConfigure.DEFAULT_NICK_NAME_FONT_SIZE;
        imageMergeAttr.nickNameFontColor = CommonImageConfigure.DEFAULT_NICK_NAME_FONT_COLOR;
        imageMergeAttr.nickNameLeft = imageMergeAttr.avatarX + imageMergeAttr.avatarWidth + 20;
        imageMergeAttr.nickNameWidth = imageMergeAttr.width - imageMergeAttr.nickNameLeft - CommonImageConfigure.DEFAULT_BOTTOM_PADDING - CommonImageConfigure.DEFAULT_QRCODE_SIZE;
        //二维码
        imageMergeAttr.qrCodeLeft = imageMergeAttr.width - CommonImageConfigure.DEFAULT_BOTTOM_PADDING - CommonImageConfigure.DEFAULT_QRCODE_SIZE;
        imageMergeAttr.qrCodeWidth = CommonImageConfigure.DEFAULT_QRCODE_SIZE;
        imageMergeAttr.qrCodeHeight = CommonImageConfigure.DEFAULT_QRCODE_SIZE;
        //主要文本
        imageMergeAttr.primaryTextFontSize = CommonImageConfigure.DEFAULT_PRIMARY_TEXT_FONT_SIZE;
        imageMergeAttr.primaryTextFontColor = CommonImageConfigure.DEFAULT_PRIMARY_TEXT_COLOR;
        imageMergeAttr.primaryTextLeft = CommonImageConfigure.DEFAULT_BOTTOM_PADDING;
        imageMergeAttr.primaryTextWidth = imageMergeAttr.width - CommonImageConfigure.DEFAULT_QRCODE_SIZE - CommonImageConfigure.DEFAULT_BOTTOM_PADDING * 2;
        //次要文本
        imageMergeAttr.secondaryTextFontSize = CommonImageConfigure.DEFAULT_SECONDARY_TEXT_FONT_SIZE;
        imageMergeAttr.secondaryTextFontColor = CommonImageConfigure.DEFAULT_SECONDARY_TEXT_COLOR;
        imageMergeAttr.secondaryTextLeft = CommonImageConfigure.DEFAULT_BOTTOM_PADDING;
        imageMergeAttr.secondaryTextWidth = imageMergeAttr.width - CommonImageConfigure.DEFAULT_QRCODE_SIZE - CommonImageConfigure.DEFAULT_BOTTOM_PADDING * 2;
        return imageMergeAttr;
    }

    /**
     * 根据图片高度更新属性
     *
     * @param imgHeight
     */
    private void updateImageMergeAttr(ImageMergeAttr imageMergeAttr, int imgHeight) {
        if (imageMergeAttr == null || imgHeight <= 0) {
            return;
        }
        imageMergeAttr.imgHeight = imgHeight;
        imageMergeAttr.height = imgHeight + CommonImageConfigure.DEFAULT_IMG_PADDING * 2 + CommonImageConfigure.DEFAULT_BOTTOM_HEIGHT;
        imageMergeAttr.avatarY = imgHeight + CommonImageConfigure.DEFAULT_IMG_PADDING * 2 + CommonImageConfigure.DEFAULT_BOTTOM_PADDING;
        imageMergeAttr.nickNameTop = imageMergeAttr.avatarY + ((CommonImageConfigure.DEFAULT_AVATAR_SIZE - CommonImageConfigure.DEFAULT_NICK_NAME_FONT_SIZE) / 2);
        imageMergeAttr.qrCodeTop = imageMergeAttr.avatarY;
        imageMergeAttr.primaryTextTop = imageMergeAttr.avatarY + CommonImageConfigure.DEFAULT_AVATAR_SIZE + 27;
        imageMergeAttr.secondaryTextTop = imageMergeAttr.primaryTextTop;
    }

    /**
     * 图片合成属性
     */
    public class ImageMergeAttr {
        //画板尺寸
        private int width;
        private int height;

        //图片尺寸
        private int imgWidth;
        private int imgHeight;
        private int imgLeft;
        private int imgTop;

        //头像尺寸
        private int avatarWidth;
        private int avatarHeight;
        private int avatarX;
        private int avatarY;

        //昵称
        private float nickNameFontSize;
        private int nickNameFontColor;
        private int nickNameLeft;
        private int nickNameTop;
        private int nickNameWidth;

        //主标题
        private float primaryTextFontSize;
        private int primaryTextFontColor;
        private int primaryTextLeft;
        private int primaryTextTop;
        private int primaryTextWidth;

        //次要标题
        private float secondaryTextFontSize;
        private int secondaryTextFontColor;
        private int secondaryTextLeft;
        private int secondaryTextTop;
        private int secondaryTextWidth;

        //二维码
        private int qrCodeLeft;
        private int qrCodeTop;
        private int qrCodeWidth;
        private int qrCodeHeight;

    }
}
